{% extends "layout.html" %} {% block title %}分析结果 - {{ filename }}{%
endblock %} {% block content %}
<header class="flex justify-between items-center mb-6">
  <div>
    <h1 class="text-3xl font-bold text-gray-900">分析结果</h1>
    <p class="text-md text-gray-600 mt-1">报告: {{ filename }}</p>
  </div>
  <a
    href="{{ url_for('index') }}"
    class="inline-flex items-center px-4 py-2 border border-gray-300 text-sm font-medium rounded-md shadow-sm text-gray-700 bg-white hover:bg-gray-50"
  >
    &larr; 分析新报告
  </a>
</header>

<div class="space-y-6">
  <!-- Report Summary -->
  <div class="card p-6">
    <h2>报告摘要</h2>
    <div class="grid grid-cols-2 md:grid-cols-4 gap-4 mt-4">
      <div class="kpi-card">
        <div class="kpi-title">数据库名</div>
        <div class="kpi-value">{{ analysis.summary.db_name }}</div>
      </div>
      <div class="kpi-card">
        <div class="kpi-title">实例名</div>
        <div class="kpi-value">{{ analysis.summary.instance_name }}</div>
      </div>
      <div class="kpi-card">
        <div class="kpi-title">快照时间 (分钟)</div>
        <div class="kpi-value">{{ analysis.summary.elapsed }}</div>
      </div>
      <div class="kpi-card">
        <div class="kpi-title">DB Time (分钟)</div>
        <div class="kpi-value">{{ analysis.summary.db_time }}</div>
      </div>
    </div>
  </div>

  <!-- Load Profile -->
  {% if analysis.load_profile.data %}
  <div class="card p-6">
    <h2>负载剖面 (Load Profile)</h2>
    <div class="overflow-x-auto mt-4">
      <table class="w-full table-auto">
        <thead>
          <tr>
            <th>指标</th>
            <th>每秒</th>
            <th>每事务</th>
          </tr>
        </thead>
        <tbody>
          {% for key, value in analysis.load_profile.data.items() %}
          <tr>
            <td class="font-medium">{{ key }}</td>
            <td>{{ value.per_second }}</td>
            <td>{{ value.per_transaction }}</td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
    {% for s in analysis.load_profile.suggestions %}
    <div class="suggestion">
      <p class="suggestion-title">{{ s.title }}</p>
      <p>{{ s.text }}</p>
    </div>
    {% endfor %}
  </div>
  {% endif %}

  <!-- Top Wait Events -->
  {% if analysis.top_waits.data %}
  <div class="card p-6">
    <h2>Top 等待事件</h2>
    <div class="overflow-x-auto mt-4">
      <table class="w-full table-auto">
        <thead>
          <tr>
            <th>事件</th>
            <th>等待次数</th>
            <th>总等待时间(s)</th>
            <th>平均等待(ms)</th>
            <th>占总DB Time %</th>
            <th>等待类别</th>
          </tr>
        </thead>
        <tbody>
          {% for row in analysis.top_waits.data %}
          <tr>
            <td>{{ row.event }}</td>
            <td>{{ row.waits }}</td>
            <td>{{ row.total_wait_time_s }}</td>
            <td>{{ row.avg_wait_ms }}</td>
            <td>{{ row.pct_total }}</td>
            <td>{{ row.get('wait_class', 'N/A') }}</td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
    {% for s in analysis.top_waits.suggestions %}
    <div class="suggestion">
      <p class="suggestion-title">{{ s.title }}</p>
      <p>{{ s.text }}</p>
    </div>
    {% endfor %}
  </div>
  {% endif %}

  <!-- Instance Efficiency -->
  {% if analysis.instance_efficiency.data %}
  <div class="card p-6">
    <h2>实例效率百分比</h2>
    <div class="overflow-x-auto mt-4">
      <table class="w-full table-auto">
        <thead>
          <tr>
            <th>指标</th>
            <th>值</th>
          </tr>
        </thead>
        <tbody>
          {% for key, value in analysis.instance_efficiency.data.items() %}
          <tr>
            <td class="font-medium">{{ key }}</td>
            <td>{{ value }}</td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
    {% for s in analysis.instance_efficiency.suggestions %}
    <div class="suggestion">
      <p class="suggestion-title">{{ s.title }}</p>
      <p>{{ s.text }}</p>
    </div>
    {% endfor %}
  </div>
  {% endif %}

  <!-- SQL Stats -->
  {% if analysis.sql_by_elapsed.data %}
  <div class="card p-6">
    <h2>SQL 统计 (按总耗时排序)</h2>
    <div class="overflow-x-auto mt-4">
      <table class="w-full table-auto">
        <thead>
          <tr>
            <th>总耗时(s)</th>
            <th>CPU时间(s)</th>
            <th>执行次数</th>
            <th>SQL ID</th>
            <th>SQL 文本 (前100字符)</th>
          </tr>
        </thead>
        <tbody>
          {% for s in analysis.sql_by_elapsed.data %}
          <tr>
            <td>{{ s.elapsed_time_s }}</td>
            <td>{{ s.cpu_time_s }}</td>
            <td>{{ s.executions }}</td>
            <td class="font-semibold text-blue-700">{{ s.sql_id }}</td>
            <td class="text-xs break-all">
              {{ s.get('sql_text', '')[:100] }}...
            </td>
          </tr>
          {% endfor %}
        </tbody>
      </table>
    </div>
    {% for s in analysis.sql_by_elapsed.suggestions %}
    <div class="suggestion">
      <p class="suggestion-title">{{ s.title }}</p>
      <p>{{ s.text }}</p>
    </div>
    {% endfor %}
  </div>
  {% endif %}
</div>
{% endblock %}
